<div class="layui-fluid">
    <div class="layui-card">
        <div id="body-div" class="layui-card-body">
        	<form id="searchForm" class="searchForm form-inline">
        		<div class="searchDiv positionSpc">
					<div class="form-group pull-left important">
						<span>流程分类</span>
						<select name="s01" component-type="select" component-id="act_category" class="form-control">
							<option value=""></option>
						</select>
					</div>
				</div>
        	</form>
        	<div id="toolbar" class="toolbar">
				<div class="pull-right">
					<a id="searchBtn" class="btn btn-success btn-bordered btn-sm"><i class="fa fa-search"></i> <font>查询</font></a>
					<a id="resetBtn" class="btn btn-success btn-bordered btn-sm"><i class="fa fa-refresh"></i> <font>重置</font></a>
					<a id="easySearch" class="btn btn-success btn-bordered btn-sm" style="display: none;"><i class="fa fa-backward"></i> <font>返回</font></a>
					<a id="hardSearch" class="btn btn-success btn-bordered btn-sm" style="display: inline-block;"><i class="fa fa-search-plus"></i> <font>高级查询</font></a>
				</div>
			</div>
            <form id="editTableForm">
				<table id="table-list"></table>
			</form>
        </div>
    </div>
</div>
<script>
    layui.use(['common', 'view', 'admin', 'laytpl'], function () {
        var $ = layui.$
            , R = layui.common
            , view = layui.view
            , admin = layui.admin
            , laytpl = layui.laytpl;

        var getTime = "-" + new Date().getTime();
        $("#body-div").attr("id", "body-div" + getTime);
        $("#table-list").attr("id","table-list" + getTime);
        var btnsStr = LayerUtil.getBtnsStrFromMenuData();

		var bootstrapTableUtil = new BootstrapTableUtil(getTime);
		bootstrapTableUtil.init({
			url: admin.basePath + '/act/process/list',
			method: 'post',
			columns: [
                {
					checkbox: true
				},
				{
					field: 'category',
					title: '流程分类',
					sortable: true,
					/*formatter:function(value, row , index){
						var result = myHelper.getValueFromDictionary("act_category",value);
						return "<a href='javascript:;' class='updateCategory'>"+result+"</a>";
					},
					events: {
						'click .updateCategory': function (e, value, row, index) {
							updateCategory({
								id: row.id
							})
						}
					},*/
				},
				{
					field: 'id',
					title: '流程ID',
					sortable: true,
				},
				{
					field: 'key',
					title: '流程标识',
					sortable: true,
				},
				{
					field: 'name',
					title: '流程名称',
					sortable: true,
					
				},
				{
					field: 'version',
					title: '流程版本',
					sortable: true,
					formatter: function(value, row, index) {
						return "V: " + value;
					}
					
				},
				{
					field: 'deploymentTime',
					title: '部署时间',
					sortable: true,
					formatter: function(value, row, index) {
						return (typeof value == "undefined" || value == null || value == "") ? "" : moment(value).format('yyyy-MM-dd HH:mm:ss'.replace(/yy/g,"YY").replace(/dd/g,"DD"));
					}
					
				},
                {
                	title: layui.admin.lang() ? 'Operation' : '操作',
                	width: '45px',
					"class": 'center must',
					events: {
						'click .active': function (e, value, row, index) {
							active({id: row.id})
						},
						'click .suspend': function (e, value, row, index) {
							suspend({
								id: row.id
							})
						},
						'click .del': function (e, value, row, index) {
							delFuc(row);
						},
						'click .toModel': function (e, value, row, index) {
							toModel({id: row.id})
						},
					},
					formatter: function(value, row, index) {
						return  '<div class="show-btn-group">'+
							'<i class="fa fa-cog" aria-hidden="true"></i>'+
							'<div class="hide-btn-group" style="width: 240px;">'+
								'<a class="btn active" dealname="激活">激活</a>'+
								'<a class="btn suspend" dealname="挂起">挂起</a>'+
								'<a class="btn del" dealname="删除">删除</a>'+
								'<a class="btn toModel" dealname="转换为模型">转换为模型</a>'+
							'</div>'+
						'</div>';
					},
                },
            ],
		},"act_process");


		$("#body-div"+getTime).find("#addBtn").on("click",function(){
			addOrEdit({});
		});

		$("#body-div"+getTime).find("#removeBtn").on("click",function(){
			var ids = bootstrapTableUtil.getMultiId();
			if(ids){
				LayerUtil.confirm(layui.admin.lang() ? "Are you sure?" : "确定删除吗？", function(){
					LayerUtil.ajax({
						type: 'post',
		        		url: admin.basePath + '/dynamic/zform/delete?formNo=act_process&ids=' + ids,
		        		success: function(res){
		        			LayerUtil.success(res[LayerUtil.getMsgLang()]);
                			bootstrapTableUtil.refresh();
		        		}
		        	})
				})
	       	}
		});

		function addOrEdit(initData) {
			var id = initData ? initData.id : '';
			LayerUtil.openDialog({
				title : layui.admin.lang() ? 'New ' : '上传流程文件',
				formLay : 'act_process',
				initData : initData,
				setDataWidthAjax : true,
				actionDataName : 'data',
				url : 'admin/act_process/form',
				//actionUrl : admin.basePath + '/dynamic/zform/getZform?formNo=oa_work_calendar&id=' + id,
	            btn : ['保存', '取消'],
	            btn_EN : ['Save', 'Cancel'],
	            func1 : function(index,layero) {
	            	var validateForm = $(layero).find("#inputForm").validate({});
	            	if(validateForm.form()){
						var data = myHelper.composeData($(layero).find("#inputForm"));
						LayerUtil.ajax({
							url : admin.basePath + '/act/process/deploy',
							type : 'post',
							data : JSON.stringify(data),
							shade: true,
							success : function(res) {
								LayerUtil.success(res[LayerUtil.getMsgLang()]);
								bootstrapTableUtil.refresh();
								layui.layer.close(index);
							}
						});
	            	}
	            }
			})
		}

		function addChild(initData) {
			var id = initData ? initData.id : '';
			LayerUtil.openDialog({
				title : layui.admin.lang() ? 'New ' : '添加个人通讯录',
				formLay : 'act_process',
				initData : initData,
				setDataWithoutAjax : true,
				actionDataName : 'data',
				url : 'admin/act_process/form',
	            btn : ['保存', '取消'],
	            btn_EN : ['Save', 'Cancel'],
	            func1 : function(index,layero) {
	            	var validateForm = $(layero).find("#inputForm").validate({});
	            	if(validateForm.form()){
						var data = myHelper.composeData($(layero).find("#inputForm"));
						LayerUtil.ajax({
							url : admin.basePath + '/dynamic/zform/save',
							type : 'post',
							data : JSON.stringify(data),
							shade: true,
							success : function(res) {
								LayerUtil.success(res[LayerUtil.getMsgLang()]);
								bootstrapTableUtil.refresh();
								layui.layer.close(index);
							}
						});
					}
	            }
			})
		}

		function updateCategory(initData) {
			$("#freeFormBody #fieldChooseModal").addClass("in").show();
			$("#freeFormBody #modal-backgroup").show();
			$("#freeFormBody #fieldChooseInput").val("").trigger("change");
		}

		function active(initData){
    		LayerUtil.confirm(layui.admin.lang() ? 'Are you sure you want to active?':'确认要激活吗？', function() {
				LayerUtil.ajax({
					url: admin.basePath + '/act/process/update/active?procDefId=' + initData.id,
					type:'post',
	        		shade:true,
					success: function(res){
	        			LayerUtil.success(res[LayerUtil.getMsgLang()]);
                			bootstrapTableUtil.refresh();
	        		}
				});
			});
    	}
		
		function suspend(initData){
    		LayerUtil.confirm(layui.admin.lang() ? 'Are you sure you want to suspend?':'确认要挂起吗？', function() {
				LayerUtil.ajax({
					url: admin.basePath + '/act/process/update/suspend?procDefId=' + initData.id,
					type:'post',
	        		shade:true,
					success: function(res){
	        			LayerUtil.success(res[LayerUtil.getMsgLang()]);
                			bootstrapTableUtil.refresh();
	        		}
				});
			});
    	}
		
		function delFuc(row){
    		LayerUtil.confirm(layui.admin.lang() ? 'Are you sure you want to delete?':'确认要删除吗？', function() {
				LayerUtil.ajax({
					url: admin.basePath + '/act/process/delete?deploymentId=' + row.deploymentId,
					type:'post',
	        		shade:true,
					success: function(res){
	        			LayerUtil.success(res[LayerUtil.getMsgLang()]);
                			bootstrapTableUtil.refresh();
	        		}
				});
			});
    	}
		
		function toModel(initData){
    		LayerUtil.confirm(layui.admin.lang() ? 'Are you sure you want to toModel?':'确认要转换成模型吗？', function() {
				LayerUtil.ajax({
					url: admin.basePath + '/act/process/toModel?procDefId=' + initData.id,
					type:'post',
	        		shade:true,
					success: function(res){
	        			LayerUtil.success(res[LayerUtil.getMsgLang()]);
                			bootstrapTableUtil.refresh();
	        		}
				});
			});
    	}
    });
</script>
